#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#include "settings.h"

#define message_c "UDP echo message"

int main(int argc, char *argv[]) {
	char message[BUFFER_SIZE];

	if (argc >= 2) {
		sprintf(message, "%s id = %s\n", message_c, argv[1]);
	} else {
		sprintf(message, "%s, id = 0\n", message_c);
	}
	printf("Client start");

	int fd_socket = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd_socket < 0) {
		perror("socket");
		return (1);
	}

	struct sockaddr_in serv_addr;
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(PORT);

	if (inet_aton(SERVER_IP, &serv_addr.sin_addr) < 0) {
		perror("inet_aton");
		return (1);
	}

	while (1) {
		char buff[BUFFER_SIZE];
		sendto(fd_socket, message, sizeof(message), 0,
				(struct sockaddr*) &serv_addr, sizeof(serv_addr));
		socklen_t len = sizeof(serv_addr);
		recvfrom(fd_socket, buff, BUFFER_SIZE, 0,
				(struct sockaddr*) &serv_addr, &len);
		printf("%s", buff);
		usleep(PAUSE);
	}
}
